import { _decorator, Component, Label, Node, ProgressBar, Sprite } from 'cc';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { GlobalData } from '../../Data/GlobalData';
import { TweenTools } from '../../Tool/TweenTools';
const { ccclass, property } = _decorator;

@ccclass('ElementLoad')
/**
 * 元素层
 */
export class ElementLoad extends BaseComponent {

    //游戏分数
    @property(Label)
    private GameScore: Label = null;

    //进度条
    @property(ProgressBar)
    private ProgreeBar: ProgressBar = null;
    //游戏操作时间
    public ControlTime: number = 0;

    //完美层
    @property(Node)
    private PrefectLoad: Node = null;

    //特殊标记
    @property(Node)
    private SpecialSign: Node = null;

    //是否是好球
    private IsGoodBall: boolean = false;
    //
    private SpecialTime: number = 0;

    //金币文字
    @property(Label)
    private CoinLabel: Label = null;

    protected Init(): void {

    }

    protected OnShow(): void {
        this.SpecialSign.active = false;
        this.PrefectLoad.active = false;

        GlobalData.GameScore = 0;
        this.UpateScore();

        GlobalData.GameCoin = 0;
        this.UpdateCoin();

        GlobalData.GameControTime = GlobalData.GameInitControlTime;
        this.ControlTime = GlobalData.GameControTime;

        this.SpecialTime = 0;
    }

    protected update(dt: number): void {
        if (this.ControlTime < 0) return;
        this.ControlTime--;
        this.ProgreeBar.progress = this.ControlTime / GlobalData.GameControTime;
    }

    /**
     * 更新分数
     */
    public UpateScore() {
        this.GameScore.string = GlobalData.GameScore.toString();
        TweenTools.ScaleBigNoSmall(this.GameScore.node, 0.1);
    }

    /**
     * 更新金币
     */
    public UpdateCoin() {
        this.CoinLabel.string = GlobalData.GameCoin.toString();
        TweenTools.ScaleBigNoSmall(this.CoinLabel.node, 0.1);
    }

    /**
     * 更新操作时间
     */
    public UpdateControlTime() {
        GlobalData.GameControTime = GlobalData.GameInitControlTime - (Math.min(GlobalData.GameScore / 2, 7) * 60);
        this.ControlTime = GlobalData.GameControTime;
    }

    /**
     * 更新得分提示
     */
    public UpdateScoreTip(_isboard: boolean, _isnet: boolean, _istop: boolean) {
        console.log("是否是空心球", _isnet)
        this.IsGoodBall = false;

        GlobalData.GameScore += 1;

        this.SpecialSign.active = false;
        if (_istop) {//高球
            this.IsGoodBall = true;
            this.SpecialSign.getComponent(Sprite).spriteFrame = app.resManager.SpriteFrames["game_gaopaosign"];
            if (!_isnet) {//高抛空心球
                this.SpecialSign.getComponent(Sprite).spriteFrame = app.resManager.SpriteFrames["game_gaokongsign"];
                if (_isboard) {//高抛擦板
                    this.SpecialSign.getComponent(Sprite).spriteFrame = app.resManager.SpriteFrames["game_gaocasign"];
                }
            }
        } else {
            if (!_isnet) {//空心球
                this.IsGoodBall = true;
                this.SpecialSign.getComponent(Sprite).spriteFrame = app.resManager.SpriteFrames["game_kongxinsign"];
                if (_isboard) {//擦板
                    this.SpecialSign.getComponent(Sprite).spriteFrame = app.resManager.SpriteFrames["game_cabansign"];
                }
            }
        }

        if (this.IsGoodBall) {//特殊显示播放动画
            this.SpecialTime++;
            GlobalData.GameScore += this.SpecialTime - 1;

            if (this.SpecialTime > 1) {
                this.PrefectLoad.active = true;
                this.PrefectLoad.getChildByName("Label").getComponent(Label).string = (this.SpecialTime - 1).toString();
                TweenTools.ScaleBig(this.PrefectLoad, 0.1);
            }

            this.SpecialSign.active = true;
            TweenTools.ScaleBig(this.SpecialSign, 0.1);
        } else {
            this.SpecialTime = 0;
        }

        this.UpateScore();

    }

}


